package com.editso.haier.intercept;

import com.editso.haier.annon.RequireLogin;
import com.editso.haier.dao.UserDao;
import com.editso.haier.result.Result;
import com.editso.haier.result.ResultCode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;

@Component
public class LoginIntercept implements Intercept{

    @Value("${spring.jackson.date-format:yyyy-mm-dd}")
    private String dateFormat;

    @Value("${haier.no-login}")
    private boolean noLogin;

    @Value("${haier.admin-user-name}")
    private String userName;

    @Value("${haier.admin-password}")
    private String password;

    @Override
    public void handlerIntercept(InterceptorRegistration registration) {
        registration.addPathPatterns("/**");
    }

    /**
     * 登陆简单拦截
     * */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Subject subject = SecurityUtils.getSubject();
        if (noLogin && !subject.isAuthenticated())
            subject.login(new UsernamePasswordToken(userName, password));
        return true;
    }

}
